package com.qiujie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qiujie.dto.Response;
import com.qiujie.dto.ResponseDTO;
import com.qiujie.entity.DailyMenu;
import com.qiujie.entity.Dish;
import com.qiujie.enums.BaseEnum;
import com.qiujie.mapper.DailyMenuMapper;
import com.qiujie.service.DailyMenuService;
import com.qiujie.service.DishService;
import com.qiujie.util.FileUtil;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.sql.Date;
import java.sql.Timestamp;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 每日食谱Service实现类
 */
@Service
public class DailyMenuServiceImpl extends ServiceImpl<DailyMenuMapper, DailyMenu> implements DailyMenuService {

    @Autowired
    private DishService dishService;

    @Value("${file-path}")
    private String uploadPath;

    @Autowired
    private FileUtil fileUtil;

    @Override
    public ResponseDTO create(DailyMenu dailyMenu) {
        boolean result = this.save(dailyMenu);
        return result ? Response.success() : Response.error("创建失败");
    }

    @Override
    public ResponseDTO update(DailyMenu dailyMenu) {
        boolean result = this.updateById(dailyMenu);
        return result ? Response.success() : Response.error("更新失败");
    }



    @Override
    public ResponseDTO getList() {
        LambdaQueryWrapper<DailyMenu> queryWrapper = new LambdaQueryWrapper<>();
        List<DailyMenu> list = this.baseMapper.selectList(queryWrapper);
        return Response.success(list);
    }

    @Override
    public ResponseDTO getDetail(Integer id) {
        DailyMenu menu = this.getById(id);
        if (menu == null) {
            return Response.error("菜单不存在");
        }
        // 可以返回一个包含菜单和饮食详情的组合对象
        return Response.success(menu);
    }

    @Override
    public ResponseDTO uploadImage(MultipartFile file) {
        try {
            // 上传图片
            String imagePath = fileUtil.upload(file, "menu");
            if (null == imagePath || "".equals(imagePath)) {
                return Response.error("更新图片失败");
            } else {
                return Response.success(imagePath);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return Response.error("上传图片失败：" + e.getMessage());
        }
    }

    @Override
    public ResponseDTO delete(Integer id) {
        boolean result = this.removeById(id);
        return result ?Response.success() : Response.error("删除失败");
    }
} 